<?php

namespace app\staff\controller\user;

use app\common\controller\StaffBase;
use app\common\library\Auth;
use think\Db;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends StaffBase
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username,nickname';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
    }
    /**
     * 關懷圈選擇用戶
     */
    public function caring_task_user($ids = 0)
    {
        $this->assignconfig("ids", $ids);
        $this->assignconfig("arr", [(int)$ids]);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->where(['user.staff_id'=>$this->auth->id])
                ->where(['v.id'=>null])
                ->join('volunteer v','v.user_id = user.id','LEFT')
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);

                $region_address = db('region')->where(['id'=>$v->province])->value('name');
                $city = db('region')->where(['id'=>$v->city])->value('name');
                if ( $city ) {
                    $region_address = $region_address.$city;
                    $district = db('region')->where(['id'=>$v->district])->value('name');
                    if ( $district ) {
                        $region_address = $region_address.$district;
                    }
                }
                $v->region_address = $region_address?$region_address:'';

            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


}
